package cn.edu.cqu.bysj.services.hibernate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.cqu.bysj.domains.QueryLeftNewDTO;
import cn.edu.cqu.bysj.domains.StaticStationInfo;
import cn.edu.cqu.bysj.domains.StopStationInfo;
import cn.edu.cqu.bysj.domains.TicketPrice;
//import cn.edu.cqu.bysj.domains.TicketPrice;
import cn.edu.cqu.bysj.services.QueryLeftNewDTOCRUD;

@Service("QueryLeftNewDTOCRUDHibernate")
public class QueryLeftNewDTOCRUDHibernate implements QueryLeftNewDTOCRUD
{
	private HibernateTemplate ht;
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		ht = new HibernateTemplate(sessionFactory);
	}
	
	@Transactional
	@Override
	public String add(QueryLeftNewDTO queryLeftNewDTO)
	{
		ht.save(queryLeftNewDTO);
		return queryLeftNewDTO.getDtoID();
	}

	@Override
	public QueryLeftNewDTO getByID(String dtoID)
	{
		return ht.get(QueryLeftNewDTO.class, dtoID);
	}

	@Override
	public List<QueryLeftNewDTO> getPlan(String fromStationName, String toStationName)
	{
		return getPlan(fromStationName, toStationName,"","");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<QueryLeftNewDTO> getAll()
	{
		List<QueryLeftNewDTO> dtos = (List<QueryLeftNewDTO>) ht.find("from QueryLeftNewDTO");
		return dtos;
	}

	@Transactional
	@Override
	public void edit(String dtoID, QueryLeftNewDTO queryLeftNewDTO)
	{
		QueryLeftNewDTO q = this.getByID(dtoID);
		q.setArriveTime(queryLeftNewDTO.getArriveTime());
		q.setFromStationCode(queryLeftNewDTO.getFromStationCode());
		q.setFromStationName(queryLeftNewDTO.getFromStationName());
		q.setStartTime(queryLeftNewDTO.getStartTime());
		q.setTaketime(queryLeftNewDTO.getTaketime());
		q.setToStationCode(queryLeftNewDTO.getToStationCode());
		q.setToStationName(queryLeftNewDTO.getToStationName());
		q.setTrainCode(queryLeftNewDTO.getTrainCode());
		q.setTransferStationCode(queryLeftNewDTO.getTransferStationCode());
		q.setTransferStationName(queryLeftNewDTO.getTransferStationName());
		ht.update(q);
	}

	@Transactional
	@Override
	public void deleteByID(String dtoID)
	{
		ht.delete(this.getByID(dtoID));
	}

	@Transactional
	@Override
	public void deleteAll()
	{
		List<QueryLeftNewDTO> dtos = this.getAll();
		for (QueryLeftNewDTO queryLeftNewDTO : dtos)
		{
			deleteByID(queryLeftNewDTO.getDtoID());
		}
	}
	
	@Override
	public List<QueryLeftNewDTO> getPlan(String fromStationName, String toStationName, String trainTypes,String userStartTime)
	{
		String fromStationCode = ht.get(StaticStationInfo.class, fromStationName).getStationCode();
		String toStationCode = ht.get(StaticStationInfo.class, toStationName).getStationCode();
		return getPlan(fromStationName, toStationName,fromStationCode,toStationCode ,trainTypes, 1,userStartTime);
	}
	
	@Override
	public List<QueryLeftNewDTO> getPlan(String fromStationName, String toStationName, String fromStationCode, String toStationCode,String trainTypes)
	{
		return getPlan(fromStationName, toStationName,fromStationCode,toStationCode ,trainTypes, 1,"");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<QueryLeftNewDTO> getPlan(String fromStationName, String toStationName, String fromStationCode, String toStationCode,String trainTypes, int GDindex,String userStartTime)
	{
		String hql = "from StopStationInfo s1, StopStationInfo s2 where s1.stationName=? and  s2.stationName=? and s1.trainNo=s2.trainNo and s1.stationNo<s2.stationNo and (";
		String[] types = trainTypes.split("-");
		int i = 0;
		for (; i < types.length - 1; i++)
		{
			hql += ("  s1.trainNo like '_____%" + types[i] + "%__'");
			hql += " or ";
		}
		hql += (" s1.trainNo like '_____%" + types[i] + "%__')");
		List<Object[]> fromList = (List<Object[]>) ht.find(hql, new Object[]{fromStationName,toStationName});
		List<QueryLeftNewDTO> ssis =  new LinkedList<>();
		int tempIndex = 0;
		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		for (Object[] objectDTO : fromList)
		{
			StopStationInfo ssi0 = (StopStationInfo)objectDTO[0];
			String trainNo = ssi0.getTrainNo();
			if (GDindex == 1&&(trainNo.contains("G")||trainNo.contains("D") ))
			{
				tempIndex = -1;
				ssis.clear();
				GDindex = 0;
			}
			try
			{
				if ((!userStartTime.equals("")) && dateFormat.parse(ssi0.getStartTime()).getTime() - dateFormat.parse(userStartTime).getTime() < 0)
				{
					continue;
				}
			} catch (ParseException e)
			{
				e.printStackTrace();
			}
			switch (tempIndex)
			{
			case -1:
				if (trainNo.contains("G")||trainNo.contains("D"))
				{
					StopStationInfo ssi1 = (StopStationInfo)objectDTO[1];
					String sql = "from TicketPrice t where t.trainNo=? and t.stationNo=?";
					List <TicketPrice> tps = (List<TicketPrice>) ht.find(sql, new Object[]{trainNo,ssi0.getStationNo()});
					TicketPrice fromTicketPrice = tps.size() == 0?new TicketPrice():tps.get(0);
					TicketPrice toTicketPrice = (TicketPrice) ht.find(sql, new Object[]{trainNo,ssi1.getStationNo()}).get(0);
					float price = Float.parseFloat(toTicketPrice.getWZ()) - Float.parseFloat(fromTicketPrice.getWZ());
					QueryLeftNewDTO queryLeftNewDTO = new QueryLeftNewDTO(trainNo,ssi0,ssi1,fromStationCode,toStationCode,"","",price);
					ssis.add(queryLeftNewDTO);
				}
				break;
			case 0:
				StopStationInfo ssi1 = (StopStationInfo)objectDTO[1];
				String sql = "from TicketPrice t where t.trainNo=? and t.stationNo=?";
				List <TicketPrice> tps = (List<TicketPrice>) ht.find(sql, new Object[]{trainNo,ssi0.getStationNo()});
				TicketPrice fromTicketPrice = tps.size() == 0?new TicketPrice():tps.get(0);
				TicketPrice toTicketPrice = (TicketPrice) ht.find(sql, new Object[]{trainNo,ssi1.getStationNo()}).get(0);
				float price = Float.parseFloat(toTicketPrice.getWZ()) - Float.parseFloat(fromTicketPrice.getWZ());
				QueryLeftNewDTO queryLeftNewDTO = new QueryLeftNewDTO(trainNo,ssi0,ssi1,fromStationCode,toStationCode,"","",price);
				ssis.add(queryLeftNewDTO);
				break;
			default:
				break;
			}
		}
		return ssis;
	}

	@SuppressWarnings("unchecked")
	@Override
	public QueryLeftNewDTO getTrain(String fromStationName, String toStationName, String trainNo)
	{
		QueryLeftNewDTO train = new QueryLeftNewDTO();
		String hql = "from StopStationInfo s1, StopStationInfo s2 where s1.stationName=? and  s2.stationName=? and s1.trainNo=? and s2.trainNo=? and s1.stationNo<s2.stationNo";
		List<Object[]> fromList = (List<Object[]>) ht.find(hql, new Object[]{fromStationName,toStationName,trainNo,trainNo});
		for (Object[] objectDTO : fromList)
		{
			StopStationInfo ssi0 = (StopStationInfo)objectDTO[0];
			StopStationInfo ssi1 = (StopStationInfo)objectDTO[1];
			String sql = "from TicketPrice t where t.trainNo=? and t.stationNo=?";
			List <TicketPrice> tps = (List<TicketPrice>) ht.find(sql, new Object[]{trainNo,ssi0.getStationNo()});
			TicketPrice fromTicketPrice = tps.size() == 0?new TicketPrice():tps.get(0);
			TicketPrice toTicketPrice = (TicketPrice) ht.find(sql, new Object[]{trainNo,ssi1.getStationNo()}).get(0);
			float price = Float.parseFloat(toTicketPrice.getWZ()) - Float.parseFloat(fromTicketPrice.getWZ());
			String fromStationCode = ht.get(StaticStationInfo.class, fromStationName).getStationCode();
			String toStationCode = ht.get(StaticStationInfo.class, toStationName).getStationCode();
			train = new QueryLeftNewDTO(trainNo,ssi0,ssi1,fromStationCode,toStationCode,"","",price);
		}
		return train;
	}

}
